package planit.view;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.AbstractAction;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import planit.controller.ControllerFactory;
import planit.controller.ControllerFactory.ControllerType;
import planit.model.Group;
import planit.model.Model;
import planit.model.Person;
import planit.view.wrappers.GroupWrapper;
import planit.view.wrappers.ModelCellRenderer;
import planit.view.wrappers.PersonWrapper;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
@SuppressWarnings("serial")
public class EditGroupDialog extends javax.swing.JDialog {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JLabel selectLabel;
	private JList groupList;
	private JScrollPane jScrollPane1;
	private JLabel descriptionLabel;
	private JScrollPane jScrollPane2;
	private AbstractAction cancelAction;
	private JButton cancelButton;
	private JList groupPersonsList;
	private JLabel groupPersonsLabel;
	private JTextField descriptionTextField;
	private JTextField nameTextField;
	private JLabel nameLabel;
	private JButton saveButton;
	
	private Model model;

	public EditGroupDialog(Model model) {
		super();
		this.model = model;
		initGUI();
		updateGroupList();
		groupPersonsList.removeAll();
	}
	
	private void initGUI() {
		try {
			{
				getContentPane().setLayout(null);
				{
					selectLabel = new JLabel();
					getContentPane().add(selectLabel);
					selectLabel.setText("Select a Group");
					selectLabel.setBounds(6, 6, 81, 16);
				}
				{
					jScrollPane1 = new JScrollPane();
					getContentPane().add(jScrollPane1);
					jScrollPane1.setBounds(6, 22, 81, 188);
					{
						ListModel groupListModel = 
							new DefaultComboBoxModel(
									new String[] { "Item One", "Item Two" });
						groupList = new JList();
						jScrollPane1.setViewportView(groupList);
						groupList.setModel(groupListModel);
						groupList.setBounds(6, 22, 81, 188);
						groupList.addListSelectionListener(new ListSelectionListener() {
							public void valueChanged(ListSelectionEvent evt) {
								groupListValueChanged(evt);
							}
						});
					}
				}
				{
					saveButton = new JButton();
					getContentPane().add(saveButton);
					saveButton.setAction(ControllerFactory
							.getController(ControllerType.EDIT_GROUP));
					saveButton.getAction().putValue("EditGroupDialog", this);
					saveButton.setText("Save");
					saveButton.setBounds(6, 216, 81, 40);
				}
				{
					nameLabel = new JLabel();
					getContentPane().add(nameLabel);
					nameLabel.setText("Name:");
					nameLabel.setBounds(93, 22, 37, 16);
				}
				{
					nameTextField = new JTextField();
					getContentPane().add(nameTextField);
					nameTextField.setBounds(93, 38, 187, 28);
				}
				{
					descriptionLabel = new JLabel();
					getContentPane().add(descriptionLabel);
					descriptionLabel.setBounds(93, 72, 68, 14);
					descriptionLabel.setText("Description");
				}
				{
					descriptionTextField = new JTextField();
					getContentPane().add(descriptionTextField);
					descriptionTextField.setBounds(93, 86, 187, 28);
				}
				{
					groupPersonsLabel = new JLabel();
					getContentPane().add(groupPersonsLabel);
					groupPersonsLabel.setText("Persons in the selected group");
					groupPersonsLabel.setBounds(93, 120, 165, 16);
				}
				{
					jScrollPane2 = new JScrollPane();
					getContentPane().add(jScrollPane2);
					jScrollPane2.setBounds(93, 135, 187, 72);
					{
						ListModel groupPersonsListModel = 
							new DefaultComboBoxModel(
									new String[] { "Item One", "Item Two" });
						groupPersonsList = new JList();
						jScrollPane2.setViewportView(groupPersonsList);
						groupPersonsList.setModel(groupPersonsListModel);
						groupPersonsList.setBounds(93, 135, 187, 72);
					}
				}
				{
					cancelButton = new JButton();
					getContentPane().add(cancelButton);
					cancelButton.setAction(getCancelAction());
					cancelButton.setText("Cancel");
					cancelButton.setBounds(219, 228, 61, 28);
				}
			}
			this.setSize(302, 300);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void groupListValueChanged(ListSelectionEvent evt) {
		if(groupList.getSelectedValue() == null) {
			nameTextField.setText("");
			descriptionTextField.setText("");
			groupPersonsList.removeAll();
		} else {
			Group temp;
			temp = model.getSchedule().getGroup(((GroupWrapper)groupList
					.getSelectedValue())
					.getId());
			
			nameTextField.setText(temp.getName());
			descriptionTextField.setText(temp.getDescription());
			updateGroupPersonsList();
			
		}
	}
	
	public void updateGroupList(){
		Group[] group = new Group[0];
		ArrayList<JLabel> allGroups = new ArrayList<JLabel>();
		group = model.getSchedule().getGroups().toArray(group);
		for (int i = 1; i < group.length; i++) {
			allGroups.add(new GroupWrapper(group[i]));
		}
		ListModel list2Model = new DefaultComboBoxModel(allGroups.toArray());
		groupList.setModel(list2Model);
		groupList.setCellRenderer(new ModelCellRenderer());
	}

	public void updateGroupPersonsList(){
		Group temp;
		temp = model.getSchedule().getGroup(((GroupWrapper)groupList
				.getSelectedValue())
				.getId());
		
		Person[] person = new Person[0];
		ArrayList<JLabel> allPersons = new ArrayList<JLabel>();
		person = temp.getPersons().toArray(person);
		for (int i = 0; i < person.length; i++) {
			allPersons.add(new PersonWrapper(person[i]));
		}
		ListModel list2Model = new DefaultComboBoxModel(allPersons.toArray());
		groupPersonsList.setModel(list2Model);
		groupPersonsList.setCellRenderer(new ModelCellRenderer());
	}
	
	private AbstractAction getCancelAction() {
		if(cancelAction == null) {
			cancelAction = new AbstractAction("Cancel", null) {
				public void actionPerformed(ActionEvent evt) {
					dispose();
				}
			};
		}
		return cancelAction;
	}
	
	public int getGroupId() {
		return ((GroupWrapper)groupList.getSelectedValue()).getId();
	}
	
	public String getGroupName() {
		return nameTextField.getText();
	}
	
	public String getGroupDescription() {
		return descriptionTextField.getText();
	}
}
